package com.example.office.util.office;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.MiniTableRenderData;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.TextRenderData;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ Author     ：jujh
 * @ Date       ：Created in 15:23 2018/12/14
 * @ Description：
 * https://www.mvnjar.com/com.deepoove/poi-tl/1.3.0/detail.html
 */
public class WordUtil {

    public static void main1(String[] args) throws Exception {
        Map<String, Object> param = new HashMap<>();
        String templatePath = "D:\\temp\\template\\wordTemplate.docx";
        String outPath = "D:\\temp\\template\\result\\1.docx";
        template2Word(param, templatePath, outPath);
    }

    public static void template2Word(Map<String, Object> param, String templatePath, String outPath) throws Exception {
        XWPFTemplate template = XWPFTemplate.compile(templatePath);
        template.render(param);
        FileOutputStream out = new FileOutputStream(outPath);
        template.write(out);
        out.flush();
        out.close();
        template.close();
    }


    public static void main(String[] args) throws Exception {
        XWPFTemplate template = XWPFTemplate.compile("D:\\temp\\template\\wordTemplate.docx");
        Map<String, Object> param = new HashMap<>();
        //标题
        param.put("title", "吃在重庆，玩在重庆");
        //表格
        RowRenderData header = RowRenderData.build(new TextRenderData("FFFFFF", "姓名"), new TextRenderData("FFFFFF", "学历"));
        List<RowRenderData> rows = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            RowRenderData row = RowRenderData.build("张三" + i, "研究生" + i);
            rows.add(row);
        }
        param.put("table", new MiniTableRenderData(header, rows));
        //本地图片
        param.put("localPicture", new PictureRenderData(120, 120, "D:\\temp\\image\\1.png"));
        template.render(param);
        FileOutputStream out = new FileOutputStream("D:\\temp\\template\\result\1.docx");
        template.write(out);
        out.flush();
        out.close();
        template.close();
    }

}
